package day0301;

public class Exer02 {

    public static void main(String[] args) {
        String str = "abcbefg";

        System.out.println(maxSubStr(str));
    }

    private static int maxSubStr(String str) {
        if (str == null || "".equals(str)){
            throw new NullPointerException("str 不能为空");
        }

        if (str.length() == 1) {
            return 1;
        }

        int max = 1;
        int start = 0, end = 1;
        for (;  end < str.length(); end ++) {
            String current = str.substring(end, end + 1);
            String sub = str.substring(start, end);

            // 当前字符是否在子串中出现过
            if (sub.contains(current)){
                // 目前子串长度大于 最大的
                if (sub.length() > max){
                    max = sub.length();
                }
                // 继续
                start = str.indexOf(current, start) + 1;

                if (str.length() - start < max) {
                    return max;
                }
            }
        }
        String sub = str.substring(start, end);
        if (sub.length() > max) {
            return sub.length();
        }
        return max;
    }
}
